package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.exception.CustomException;
import com.tencreat.common.utils.DateUtils;
import com.tencreat.common.utils.TenCreatUtil;
import com.tencreat.domain.mall.MallGoods;
import com.tencreat.domain.mall.MallGoodsCollage;
import com.tencreat.domain.mall.MallGoodsKill;
import com.tencreat.domain.mall.dto.MallGoodsCollageDTO;
import com.tencreat.domain.mall.dto.MallGoodsRecordDTO;
import com.tencreat.domain.mall.vo.MallGoodsKIllRecordVo;
import com.tencreat.domain.mall.vo.MallGoodsKillStatisticsVO;
import com.tencreat.domain.mall.vo.MallGoodsKillVO;
import com.tencreat.service.mall.IMallGoodsKillService;
import com.tencreat.service.mall.IMallGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "管理端秒杀相关api")
//@RestController
@RequestMapping("/mall/pc/kill")
public class PCMallKillController extends BaseController {
    //@Autowired
    private IMallGoodsKillService mallGoodsKillService;

    //@Autowired
    private IMallGoodsService mallGoodsService;


    /**
     * 新增商品秒杀
     */
    @ApiOperation(value = "新增商品秒杀")
    @Log(title = "营销管理>秒杀管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody List<MallGoodsKill> mallGoodsKill) {
        int sums = 0;
        Date now = new Date();
        for (MallGoodsKill goodsKill : mallGoodsKill) {
            if (goodsKill.getStartTime().isBefore(LocalDateTime.now())) {
                return HttpResult.error("开始时间不能小于当前时间");
            }

            if (goodsKill.getEndTime().isBefore(LocalDateTime.now())) {
                return HttpResult.error("结束时间不能小于当前时间");
            }
            List<MallGoodsKill> kills = mallGoodsKillService.lambdaQuery()
                    .eq(MallGoodsKill::getGoodsId, goodsKill.getGoodsId()).list();
            for (MallGoodsKill kill:kills) {
                if (kill != null) {
                    Date begin = Date.from(kill.getCreateTime().atZone(ZoneId.systemDefault()).toInstant());
                    Date end = Date.from(kill.getEndTime().atZone(ZoneId.systemDefault()).toInstant());
                    Date newEnd = Date.from(goodsKill.getEndTime().atZone(ZoneId.systemDefault()).toInstant());

                    if (DateUtils.isEffectiveDate(now, begin, end) || DateUtils.isEffectiveDate(newEnd, begin, end)) {
                        throw new CustomException(goodsKill.getGoodsId() + "商品已是秒杀商品,不能重复添加,请重试! 秒杀开始时间:" + goodsKill.getCreateTime() + ",秒杀结束时间:" + goodsKill.getEndTime());
                    }
                }
            }
            MallGoods mallGoods = mallGoodsService.lambdaQuery().eq(MallGoods::getId, goodsKill.getGoodsId()).one();
            goodsKill.setGoodsSn(mallGoods.getGoodsSn());
            goodsKill.setInventory(100000L);
            goodsKill.setActiveSn(TenCreatUtil.getOrderNo());
            goodsKill.setOriginPrice(mallGoods.getRetailPrice());
            goodsKill.setStatus(1);
            sums += mallGoodsKillService.insertMallGoodsKill(goodsKill);
        }
        return to(sums);
    }

    /**
     * 修改商品秒杀
     */
    @ApiOperation(value = "修改商品秒杀")
    @Log(title = "营销管理>秒杀管理", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody MallGoodsKill mallGoodsKill) {
        return to(mallGoodsKillService.updateMallGoodsKill(mallGoodsKill));
    }

    /**
     * 删除商品秒杀
     */
    @ApiOperation(value = "删除商品秒杀")
    @Log(title = "营销管理>秒杀管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody ReqIdsParam ids) {
        return to(mallGoodsKillService.deleteMallGoodsKillByIds(ids));
    }

    /**
     * pc查询秒杀商品列表
     */
    @ApiOperation(value = "pc管理端查询秒杀商品列表")
    @PostMapping("/getPCList")
    public HttpResult<IPage<MallGoodsKillVO>> list(@RequestBody MallGoodsCollageDTO dto) {
        return HttpResult.ok(mallGoodsKillService.getPCList(generatePage(dto),dto));
    }

    /**
     * pc管理查询秒杀商品详情
     */
    @ApiOperation("pc管理查询秒杀商品详情")
    @GetMapping("/getPCDetails")
    public HttpResult<Map<String, Object>> getPCDetails(@ApiParam("秒杀商品详情") @RequestParam Long id) {
        return mallGoodsKillService.getPCDetails(id);
    }

    @ApiOperation("秒杀管理金额统计")
    @GetMapping("/killStatistics")
    public HttpResult<MallGoodsKillStatisticsVO> killStatistics(){
        return HttpResult.ok(mallGoodsKillService.killStatistics());
    }

    @ApiOperation("秒杀记录")
    @PostMapping("/killRecord")
    public HttpResult<IPage<MallGoodsKIllRecordVo>> killRecord(@RequestBody MallGoodsRecordDTO dto){
        IPage<MallGoodsKIllRecordVo> iPage = mallGoodsKillService.killRecord(generatePage(dto),dto);
        return HttpResult.ok(iPage);
    }

    /*****
     *  将当前分类的排序减1 顺序上升一位
     * @param mallGoodsKill
     * @return
     */
    @PostMapping("/reSortOrderReduce")
    public HttpResult<Boolean> reSortOrderReduce(@RequestBody MallGoodsKill mallGoodsKill){
        return HttpResult.ok(mallGoodsKillService.reSortOrderReduce(mallGoodsKill));
    }

    /*****
     * 将当前分类的排序加1 顺序下降一位
     * @param mallGoodsKill
     * @return
     */
    @PostMapping("/reSortOrderPlus")
    public HttpResult<Boolean> reSortOrderPlus(@RequestBody MallGoodsKill mallGoodsKill){
        return HttpResult.ok(mallGoodsKillService.reSortOrderPlus(mallGoodsKill));
    }
}
